import fetch from "node-fetch";
import * as cheerio from "cheerio";

async function fetchMirrorWebsite(url: string) {
  const resp = await fetch(url);
  const html = await resp.text();
  const $ = cheerio.load(html);
  const aTags = $("pre a");
  for (const aTag of aTags) {
    if (aTag.firstChild?.type === "text") {
      let name = aTag.firstChild.nodeValue;
      if (/^\d+\.\d+\.\d+\/$/.test(name)) {
        name = name.substring(0, name.length - 1);
        let lastModified: string | undefined;
        let size: number | undefined;
        if (aTag.next?.type === "text") {
          const c = aTag.next.nodeValue.trim();
          const [date, time, sizeStr] = c.split(/\s+/g);
          lastModified = `${date.trim()} ${time.trim()}`;
          size = sizeStr && sizeStr !== "-" ? +sizeStr.trim() : undefined;
        }
        console.log("name =", name, ", lastModified =", lastModified, ", size =", size);
      }
    }
  }
}

// fetchMirrorWebsite("https://mirrors.huaweicloud.com/apache/maven/maven-3/");
// fetchMirrorWebsite("https://mirrors.cloud.tencent.com/apache/maven/maven-3/");
fetchMirrorWebsite("https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/");
// fetchMirrorWebsite("https://dlcdn.apache.org/maven/maven-3/");

// https://mirrors.aliyun.com/apache/maven/maven-3/3.9.9/binaries/apache-maven-3.9.9-bin.zip
